package com.itfreer.arcgis.edit;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.itfreer.arcgis.draw.DrawTool;
import com.itfreer.arcgis.draw.DrawToolManager;
import com.itfreer.arcgis.draw.DrawToolType;
import com.itfreer.arcgis.layer.LayerLoader;
import com.itfreer.arcgis.utils.FeatureTableUtils;
import com.itfreer.arcgis.utils.GeometryUtils;
import com.itfreer.arcgis.utils.MapUtils;
import com.itfreer.gis.datasyn.entity.ProjectEntity;
import com.itfreer.ui.base.DialogCloseListener;
import com.itfreer.ui.base.domain.DomainRadioDialog;
import com.itfreer.utils.common.StringUtils;
import com.itfreer.utils.common.ToastUtils;
import com.itfreer.utils.config.ConfigManager;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class EditToolUtils
{
    public  final  static  String eidtFeatureLayerKey="editFeatureLayer";
    /**
     * 弹出选择可编辑图层对话框
     */
    public static void showSelectEditLayer(List<FeatureLayer> featureLayers,DialogCloseListener dialogCloseListener)
    {
        if(featureLayers == null || featureLayers.size()==0)
        {
            return;
        }
        //编辑可编辑的图层选择编辑图层
        LinkedHashMap<String,String> data = new LinkedHashMap<String, String>();
        for(Integer i =0;i<featureLayers.size();i++)
        {
            String id = LayerLoader.instance.getConfig(featureLayers.get(i)).getId();
            String layerName = LayerLoader.instance.getConfig(featureLayers.get(i)).getLayerName();
            data.put(id,layerName);
        }
        DomainRadioDialog radioDialog = new DomainRadioDialog(ConfigManager.getContext());
        radioDialog.setDialogTitle("选择编辑图层");
        radioDialog.setBindData(data);
        radioDialog.setDialogCloseListener(dialogCloseListener);
        radioDialog.show();
    }

    public static List<FeatureLayer> getEditLayer(MapView mapView)
    {
        List<FeatureLayer> allFeatureLayers = MapUtils.GetFeatureLayers(mapView);
        List<FeatureLayer> editFeatureLayers = new ArrayList<>();
        for(Integer i = 0;i<allFeatureLayers.size();i++)
        {
            ProjectEntity entity = LayerLoader.instance.getConfig(allFeatureLayers.get(i));
            //不为空、可见、可编辑的图层加入选择编辑图层列表
            if(entity !=null && entity.getShow() && entity.getCanEdit() && allFeatureLayers.get(i).getFeatureTable().canAdd())
            {
                editFeatureLayers.add(allFeatureLayers.get(i));
            }
        }
        return  editFeatureLayers;
    }

    public static DrawTool getDrawToolByCurrentEditLayer()
    {
        Object data = ConfigManager.getConfig(EditToolUtils.eidtFeatureLayerKey);
        if(data != null)
        {
            FeatureLayer featureLayer = (FeatureLayer)data;
            if(featureLayer !=null)
            {
                if(FeatureTableUtils.isPointFeatuerTable(featureLayer.getFeatureTable()))
                    return new CreatePointTool();
                else if(FeatureTableUtils.isPolylineFeatuerTable(featureLayer.getFeatureTable()))
                    return new CreatePolylineTool();
                else if(FeatureTableUtils.isPolygonFeatuerTable(featureLayer.getFeatureTable()))
                    return new CreatePolygonTool();
                else
                    return  null;
            }
        }
        return  null;
    }

    /**
     * 获取绘画对象
     * @return
     */
    public static void updateDrawTool(MapView mapView)
    {
        Object data = ConfigManager.getConfig(EditToolUtils.eidtFeatureLayerKey);
        if(data != null)
        {
            FeatureLayer featureLayer = (FeatureLayer) data;
            if(featureLayer !=null)
            {
                if(FeatureTableUtils.isPointFeatuerTable(featureLayer.getFeatureTable()))
                {
                    if(DrawToolManager.GetCurrentTool() ==null || DrawToolManager.GetCurrentTool().GetGeometry() ==null
                        || DrawToolManager.GetCurrentTool().GetGeometry().isEmpty() ||
                            !(DrawToolManager.GetCurrentTool() instanceof CreatePointTool))
                        new CreatePointTool().Draw(mapView,null);
                }
                else if(FeatureTableUtils.isPolylineFeatuerTable(featureLayer.getFeatureTable()))
                {
                    if(DrawToolManager.GetCurrentTool() ==null || DrawToolManager.GetCurrentTool().GetGeometry() ==null
                            || DrawToolManager.GetCurrentTool().GetGeometry().isEmpty() ||
                            !(DrawToolManager.GetCurrentTool() instanceof CreatePolylineTool))
                        new CreatePolylineTool().Draw(mapView,null);
                }
                else if(FeatureTableUtils.isPolygonFeatuerTable(featureLayer.getFeatureTable()))
                {
                    if(DrawToolManager.GetCurrentTool() ==null || DrawToolManager.GetCurrentTool().GetGeometry() ==null
                            || DrawToolManager.GetCurrentTool().GetGeometry().isEmpty() ||
                            !(DrawToolManager.GetCurrentTool() instanceof CreatePolygonTool))
                        new CreatePolygonTool().Draw(mapView,null);
                }
            }
            else
            {
                ToastUtils.showShort(mapView.getContext(), "请先选择编辑图层！");
            }
        }
        else
        {
            ToastUtils.showShort(mapView.getContext(), "请先选择编辑图层！");
        }
    }

    public static FeatureLayer getEditSelectFeatureLayerById(MapView mapView, String id)
    {
        if(StringUtils.IsNullOrEmpty(id))
            return  null;
        List<FeatureLayer> featureLayers = getEditLayer(mapView);
        if(featureLayers == null || featureLayers.size() == 0)
            return  null;
        for(Integer i = 0;i<featureLayers.size();i++)
        {
            ProjectEntity entity = LayerLoader.instance.getConfig(featureLayers.get(i));
            if(entity !=null)
            {
                if (entity.getId().equals(id))
                    return featureLayers.get(i);
            }
        }
        return  null;
    }

    public static  FeatureLayer getCurrentEditFeatureLayer()
    {
        Object data = ConfigManager.getConfig(eidtFeatureLayerKey);
        if(data !=null)
        {
            FeatureLayer featureLayer = (FeatureLayer)data;
            return  featureLayer;
        }
        return  null;
    }

    /**
     * 根据绘画工具类型和点集合获取Geometry 对象
     * @param toolType
     * @param points
     * @param spatialReference
     * @return
     */
    public static Geometry getGeometryByDrawTypeAndPoints(DrawToolType toolType, List<Point> points, SpatialReference spatialReference)
    {
        if(toolType ==DrawToolType.Polyline)
            return GeometryUtils.pointsToPolyline(points,spatialReference);
        else if(toolType == DrawToolType.Polygon)
            return GeometryUtils.pointsToPolygon(points,spatialReference);
        return  null;
    }
}
